package com.lengxf.mqtt.mqttTest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lengxf.mqtt.config.outbound.UserMessagingGateway;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * emq 测试
 *
 * @Author Lengxf
 */
@RestController
@RequestMapping
@AllArgsConstructor
public class MqttTestController {

    private final UserMessagingGateway userMessagingGateway;
    private final ObjectMapper objectMapper;

    @GetMapping("publish")
    public String publish() throws JsonProcessingException {
        String topic = "sys/product/123/status";
        String msg = """
                {"tid":"6a7bfe89-c386-4043-b600-b518e10096cc","bid":"42a19f36-5117-4520-bd13-fd61d818d52e","timestamp":1598411295123,"gateway":"sn","data":{"device_secret":"device_secret","domain":0,"nonce":"nonce","sn":"sn","sub_devices":[{"device_secret":"device_secret","domain":0,"index":"index","nonce":"nonce","sn":"sn","sub_type":0,"type":0,"version":0}],"sub_type":0,"type":0,"version":0}}""";
        String s = StringEscapeUtils.unescapeJava(msg);
        userMessagingGateway.publish(topic, objectMapper.writeValueAsBytes(s));
        return "success";
    }


}
